package com.data.modules.smzh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.data.common.api.vo.Result;
import com.data.common.aspect.annotation.AutoLog;
import com.data.common.system.base.controller.BootController;
import com.data.common.system.query.QueryGenerator;
import com.data.modules.smzh.entity.SmMessage;
import com.data.modules.smzh.service.ISmMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 时迈消息表
 * @Author: data-boot
 * @Date:   2022-12-17
 * @Version: V1.0
 */
@Api(tags="时迈消息表")
@RestController
@RequestMapping("/smzh/smMessage")
@Slf4j
public class SmMessageController extends BootController<SmMessage, ISmMessageService> {
	@Autowired
	private ISmMessageService smMessageService;

	/**
	 * 分页列表查询
	 *
	 * @param smMessage
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "时迈消息表-分页列表查询")
	@ApiOperation(value="时迈消息表-分页列表查询", notes="时迈消息表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(SmMessage smMessage,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SmMessage> queryWrapper = QueryGenerator.initQueryWrapper(smMessage, req.getParameterMap());
		Page<SmMessage> page = new Page<SmMessage>(pageNo, pageSize);
		IPage<SmMessage> pageList = smMessageService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 //@AutoLog(value = "时迈消息表-分页列表查询")
	 @ApiOperation(value="时迈消息表-分页列表查询", notes="时迈消息表-分页列表查询")
	 @PostMapping(value = "/queryMessagelist")
	 public Result<?> queryMessagelist( @RequestBody SmMessage smMessage,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="50") Integer pageSize) {

		/* QueryWrapper<SmMessage> queryWrapper = QueryGenerator.initQueryWrapper(smMessage, req.getParameterMap());
		 Page<SmMessage> page = new Page<SmMessage>(pageNo, pageSize);
		 IPage<SmMessage> pageList = smMessageService.page(page, queryWrapper);*/

		 List<SmMessage> list = smMessageService.lambdaQuery().eq(SmMessage::getModile,smMessage.getModile()).eq(SmMessage::getStatus,0).list();

		 return Result.OK(list);
	 }

	@AutoLog(value = "时迈消息表-编辑")
	@ApiOperation(value="时迈消息表-编辑", notes="时迈消息表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody SmMessage smMessage) {
		smMessageService.updateById(smMessage);
		return Result.OK("编辑成功!");
	}


	/**
	 *   添加
	 *
	 * @param smMessage
	 * @return
	 */
	@AutoLog(value = "时迈消息表-添加")
	@ApiOperation(value="时迈消息表-添加", notes="时迈消息表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody SmMessage smMessage) {
		smMessageService.save(smMessage);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param smMessage
	 * @return
	 */
	@AutoLog(value = "时迈消息表-标记已读")
	@ApiOperation(value="时迈消息表-标记已读", notes="时迈消息表-标记已读")
	@PostMapping(value = "/editRead")
	public Result<?> editRead(@RequestBody SmMessage smMessage) {

		SmMessage message = new SmMessage();
		message.setId(smMessage.getId());
		message.setStatus(1);
		smMessageService.updateById(message);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "时迈消息表-通过id删除")
	@ApiOperation(value="时迈消息表-通过id删除", notes="时迈消息表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		smMessageService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "时迈消息表-批量删除")
	@ApiOperation(value="时迈消息表-批量删除", notes="时迈消息表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.smMessageService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "时迈消息表-通过id查询")
	@ApiOperation(value="时迈消息表-通过id查询", notes="时迈消息表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		SmMessage smMessage = smMessageService.getById(id);
		if(smMessage==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(smMessage);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param smMessage
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SmMessage smMessage) {
        return super.exportXls(request, smMessage, SmMessage.class, "时迈消息表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SmMessage.class);
    }

}
